package com.suse.lhw.hms.exersiseserver.service.Impl;

import com.suse.lhw.hms.exersiseserver.constants.ConstStatuMovementModel;
import com.suse.lhw.hms.exersiseserver.dao.ECollectionMapper;
import com.suse.lhw.hms.exersiseserver.dao.EModelsOfPackageMapper;
import com.suse.lhw.hms.exersiseserver.dao.EPackageToSingleMmMapper;
import com.suse.lhw.hms.exersiseserver.domin.*;
import com.suse.lhw.hms.exersiseserver.pojo.out.MovementModelAndUnits;
import com.suse.lhw.hms.exersiseserver.pojo.out.PackageAndModels;
import com.suse.lhw.hms.exersiseserver.service.EPackageToSingleMmService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.suse.lhw.hms.exersiseserver.dao.EPackageMapper;
import com.suse.lhw.hms.exersiseserver.service.EPackageService;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.exersiseserver.service.Impl
 * @ClassName: EPackageServiceImpl
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/5/3 13:59
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/3 13:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
public class EPackageServiceImpl implements EPackageService {

    @Resource
    private EPackageMapper ePackageMapper;
    @Resource
    private EPackageToSingleMmMapper ePackageToSingleMmMapper;
    @Resource
    private EModelsOfPackageMapper eModelsOfPackageMapperl;
    @Resource
    private ECollectionMapper eCollectionMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return ePackageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(EPackage record) {
        return ePackageMapper.insert(record);
    }

    @Override
    public int insertSelective(EPackage record) {
        return ePackageMapper.insertSelective(record);
    }

    @Override
    public EPackage selectByPrimaryKey(Integer id) {
        return ePackageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(EPackage record) {
        return ePackageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(EPackage record) {
        return ePackageMapper.updateByPrimaryKey(record);
    }

    @Override
    public int insertAPackageAndModels(EPackage ePackage, List<EModelsOfPackage> list) {
        //先添加一个运动套餐
        if (ePackageMapper.insertSelective(ePackage) == 0)
            return 0;
        //添加套餐中的米一个单独动作
        for (EModelsOfPackage ePackageToSingleMm : list) {
            if (eModelsOfPackageMapperl.insertSelective(ePackageToSingleMm) == 0)
                return 0;
        }
        return 1;
    }

    @Override
    public List<PackageAndModels> selectAllPackageByStatus(int defaultt) {
        //先通过status找到相应的套餐
        List<EPackage> packageList = ePackageMapper.insertAllByStatus(defaultt);
        List<PackageAndModels> resultList = new ArrayList<PackageAndModels>();
        if (packageList.isEmpty())
            return null;
        //在通过套餐的id找每个模型对应，封装成输出list
        for (EPackage ePackage : packageList) {
            List<EModelsOfPackage> eptsm = eModelsOfPackageMapperl.selectAllByPackageId(ePackage.getId());
            PackageAndModels packageAndModels = new PackageAndModels();
            packageAndModels.setCategoryid(ePackage.getCategoryid());
            packageAndModels.setDescription(ePackage.getDescription());
            packageAndModels.setImgurl(ePackage.getImgurl());
            packageAndModels.setName(ePackage.getName());
            packageAndModels.setStatus(ePackage.getStatus());
            packageAndModels.setVideourl(ePackage.getVideourl());
            packageAndModels.setList(eptsm);
            resultList.add(packageAndModels);
        }
        return resultList;

    }

    @Override
    public int deleteAllById(int id) {
        //删除一个套餐，你要删除套餐字段和套餐里的映射字段
        //删除套餐
        if (ePackageMapper.deleteByPrimaryKey(id) == 0)
            return 0;
        //找出所有映射并删除
        List<EPackageToSingleMm> ePackageToSingleMmList = ePackageToSingleMmMapper.selectByPakcageId(id);
        for (EPackageToSingleMm ePackageToSingleMm : ePackageToSingleMmList) {
            if (ePackageToSingleMmMapper.deleteByPrimaryKey(ePackageToSingleMm.getId()) == 0)
                return 0;

        }
        return 1;
    }

    @Override
    public PackageAndModels selectAPackageById(int id) {
        PackageAndModels packageAndModels = new PackageAndModels();
        //找出对应的套餐
        EPackage ePackage = ePackageMapper.selectByPrimaryKey(id);
        //找出对应的模型
        List<EModelsOfPackage> lsit = eModelsOfPackageMapperl.selectAllByPackageId(id);
        packageAndModels.setVideourl(ePackage.getVideourl());
        packageAndModels.setStatus(ePackage.getStatus());
        packageAndModels.setName(ePackage.getName());
        packageAndModels.setImgurl(ePackage.getImgurl());
        packageAndModels.setDescription(ePackage.getDescription());
        packageAndModels.setList(lsit);

        return packageAndModels;
    }

    @Override
    public List<PackageAndModels> selectCollectedBYUseridType(String userId, int package1) {
        //查找所有的套餐收藏
        //先找收藏
        List<ECollection> list = eCollectionMapper.selectByUserIdAndType(userId, package1);
        List<PackageAndModels> returnList = new ArrayList<PackageAndModels>();
        for (ECollection eCollection : list) {
            PackageAndModels qpackageAndModels = new PackageAndModels();
            EPackage ePackage = ePackageMapper.selectByPrimaryKey(eCollection.getCollectionid());
            List<EModelsOfPackage> eModelsOfPackages = eModelsOfPackageMapperl.selectAllByPackageId(eCollection.getCollectionid());
            qpackageAndModels.setImgurl(ePackage.getImgurl());
            qpackageAndModels.setName(ePackage.getName());
            qpackageAndModels.setDescription(ePackage.getDescription());
            qpackageAndModels.setVideourl(ePackage.getVideourl());
            qpackageAndModels.setStatus(ePackage.getStatus());
            qpackageAndModels.setCategoryid(ePackage.getCategoryid());
            qpackageAndModels.setList(eModelsOfPackages);
            returnList.add(qpackageAndModels);
        }
        return returnList;

    }

    @Override
    public List<PackageAndModels> selectCollectedBYUseridTypeName(String userId, int type, String name) {
        List<PackageAndModels> result = new ArrayList<PackageAndModels>();
        List<EModelsOfPackage> eModelsOfPackages = new ArrayList<EModelsOfPackage>();
        //如果传来的type是default，没有搜名字,则不需要userid
        if (type == ConstStatuMovementModel.defaultt && name == null) {
            List<EPackage> mms = ePackageMapper.selectAllByStatus(ConstStatuMovementModel.defaultt);
            for (EPackage mm : mms) {
                PackageAndModels packageAndModels = new PackageAndModels();
                eModelsOfPackages = eModelsOfPackageMapperl.selectAllByPackageId(mm.getId());
                packageAndModels.setImgurl(mm.getImgurl());
                packageAndModels.setName(mm.getName());
                packageAndModels.setDescription(mm.getDescription());
                packageAndModels.setVideourl(mm.getVideourl());
                packageAndModels.setStatus(mm.getStatus());
                packageAndModels.setCategoryid(mm.getCategoryid());
                packageAndModels.setList(eModelsOfPackages);

                result.add(packageAndModels);

            }
        }
        //如果传来的type是default，用了搜索框则需要用name属性
        if (type == ConstStatuMovementModel.defaultt && name != null) {
            List<EPackage> mms = ePackageMapper.selectByTypeName(ConstStatuMovementModel.defaultt, name);
            for (EPackage mm : mms) {
                PackageAndModels packageAndModels = new PackageAndModels();
                eModelsOfPackages = eModelsOfPackageMapperl.selectAllByPackageId(mm.getId());
                packageAndModels.setImgurl(mm.getImgurl());
                packageAndModels.setName(mm.getName());
                packageAndModels.setDescription(mm.getDescription());
                packageAndModels.setVideourl(mm.getVideourl());
                packageAndModels.setStatus(mm.getStatus());
                packageAndModels.setCategoryid(mm.getCategoryid());
                packageAndModels.setList(eModelsOfPackages);

                result.add(packageAndModels);

            }

        }
        //传来的type是用户自定义的，没用搜索框
        if ((type == ConstStatuMovementModel.un_upload || type == ConstStatuMovementModel.uploaded) && name == null) {
            List<EPackage> mms = ePackageMapper.selectByTypeUserid(ConstStatuMovementModel.defaultt, userId);
            for (EPackage mm : mms) {
                PackageAndModels packageAndModels = new PackageAndModels();
                eModelsOfPackages = eModelsOfPackageMapperl.selectAllByPackageId(mm.getId());
                packageAndModels.setImgurl(mm.getImgurl());
                packageAndModels.setName(mm.getName());
                packageAndModels.setDescription(mm.getDescription());
                packageAndModels.setVideourl(mm.getVideourl());
                packageAndModels.setStatus(mm.getStatus());
                packageAndModels.setCategoryid(mm.getCategoryid());
                packageAndModels.setList(eModelsOfPackages);

                result.add(packageAndModels);

            }
        }
        //传来的type是用户自定义的，且用了搜索框
        if ((type == ConstStatuMovementModel.un_upload || type == ConstStatuMovementModel.uploaded) && name != null) {
            List<EPackage> mms = ePackageMapper.selectByTypeUseridName(ConstStatuMovementModel.defaultt, userId, name);
            for (EPackage mm : mms) {
                PackageAndModels packageAndModels = new PackageAndModels();
                eModelsOfPackages = eModelsOfPackageMapperl.selectAllByPackageId(mm.getId());
                packageAndModels.setImgurl(mm.getImgurl());
                packageAndModels.setName(mm.getName());
                packageAndModels.setDescription(mm.getDescription());
                packageAndModels.setVideourl(mm.getVideourl());
                packageAndModels.setStatus(mm.getStatus());
                packageAndModels.setCategoryid(mm.getCategoryid());
                packageAndModels.setList(eModelsOfPackages);

                result.add(packageAndModels);

            }
        }
        return result;
    }
}




